(function($) {
	var defaults = {
		menus : [
		],
		home:"../home/index.html",
		logotitle:"test",
		logoimg:"../../images/logo.png",
		searchFormAction:"",
		searchInputDefaultValue:"搜索...",
		islogin:false,
		loginame:''
	};

	var methods = {
		init : function(opts) {
			this.uiHeader = $(this);
			this.uiHeader.addClass("header");
			this.uiLogoPanel = $('.logo', this.uiHeader);
			if (this.uiLogoPanel.length == 0) {
				this.uiLogoPanel = $('<div></div>').addClass("logo");
				$('<a href="'+opts.home+'"> <span class="hidden">'+opts.logotitle+'</span> <img src="'+opts.logoimg+'" alt="'+opts.logotitle+'" /></a>').appendTo(this.uiLogoPanel);
				this.uiLogoPanel.appendTo(this.uiHeader);
			}
			this.uiBarPanel = $('.menu', this.uiHeader);
			if (this.uiBarPanel.length == 0) {
				this.uiBarPanel = $('<div></div>').addClass("menu");
				var ul = methods.createMenu.apply(this, [
					opts.menus
				]);
				if (ul != null) {
					ul.appendTo(this.uiBarPanel);
					$('ul li', this.uiBarPanel).hover(function() {
						$('ul', this).fadeIn(300);
					}, function() {
						$('ul', this).fadeOut(300);
					});
					$('ul li ul li', this.uiBarPanel).not('.active').hover(function() {
						$(this).animate({
							textIndent : "14px"
						}, 150);
					}, function() {
						$(this).animate({
							textIndent : "0"
						});
					});
				}
				this.uiBarPanel.appendTo(this.uiHeader);
			}
			this.uiSearchPanel = $('.search', this.uiHeader);
			if (this.uiSearchPanel.length == 0) {
				this.uiSearchPanel =  $('<div></div>').addClass("search");
				var tr = $('<tr></tr>');
				var form = $('<form action="'+opts.searchFormAction+'" method="post" ></form>');
				var input = $('<input type="text" name="searchbox" value="'+opts.searchInputDefaultValue+'" onfocus="if (this.value==this.defaultValue) this.value=\'\';" onblur="if (this.value==\'\') this.value=\''+opts.searchInputDefaultValue+'\'" />').addClass("searchbox");
				var btn = $('<input type="submit" value="" id="search-bt" /> ').addClass('search-bt');
				var loginform = $('<form method="post" ><input type="hidden" name="redirect" value="'+window.location.href+'"/></form>');
				var loginbtn = $('<input name="submit" type="submit"  value="登录" />').addClass("loginsubmit");
				var loginuser = $('<span >'+opts.loginame+',欢迎使用</span>').addClass("login");
				var logoutbtn = $('<input name="submit" type="submit"  value="退出" />').addClass("loginsubmit");
				input.appendTo(form);
				btn.appendTo(form);
				if (opts.islogin){
					loginform.attr("action","../home/logoff");
					loginuser.appendTo(loginform);
					logoutbtn.appendTo(loginform);
				}else{
					loginform.attr("action","../home/login");
					loginbtn.appendTo(loginform);
				}
				var td = $('<td></td>');
				form.appendTo(td);
				td.appendTo(tr);
				td = $('<td style="vertical-align: top"></td>');
				loginform.appendTo(td);
				td.appendTo(tr);
				var table = $('<table></table>');
				tr.appendTo(table);
				table.appendTo(this.uiSearchPanel);
				this.uiSearchPanel.appendTo(this.uiHeader);
				$('<div></div>').addClass("clear").appendTo(this.uiHeader);
			}
			if (this.uiHeader.attr("menuid") != null){
				methods.setMenu.apply(this,[this.uiHeader.attr("menuid")]);
			}
		} ,
		createMenu : function(menus) {
			var self = this;
			if (menus != null) {
				var ul = $("<ul></ul>");
				$.each(menus, function() {
					var li = $('<li id="' + this.id + '"></li>');
					if (this.active) {
						li.addClass("active");
					}
					$('<a href="' + this.href + '">' + this.title + '</a>').appendTo(li);
					if (this.children != null) {
						var children = methods.createMenu.apply(self, [
							this.children
						]);
						if (children != null) children.appendTo(li);
					}
					li.appendTo(ul);
				});
				return ul;
			} else {
				return null;
			}
		} ,
		setMenu : function(id) {
			var thisMenu = $(this);
			$("ul > li", thisMenu).each(function() {
				$(this).removeClass("active");
				if (this.id == id) {
					$(this).addClass("active");
				}
			});
		}
	};
	$.fn.mainHeader = function(opts) {
		var options = $.extend(defaults, opts);
		var args = arguments;
		return this.each(function(index) {
			if (methods[opts]) {
				var ar = Array.prototype.slice.call(args, 1);
				return methods[opts].apply(this, ar);
			} else if (typeof opts === 'object' || !opts) {
				return methods.init.apply(this, [
					options
				]);
			} else {
				$.error('Method ' + method + ' does not exist on jQuery.tooltip');
			}
		});
	};
})(jQuery);